﻿using PlantsZombies.View.ViewEx;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace PlantsZombies.Common.Card
{
    public delegate void DrawCardDelegate(CardObject cardObject, CardEventArgs args);
    public abstract class CardObject
    {
        private readonly ImageObject _imageObject;
        private readonly SpeciesType _speciesType;
        private readonly Size _size;
        public CardObject(ImageObject imageObject, SpeciesType speciesType, Size size)
        {
            _imageObject = imageObject;
            _speciesType = speciesType;
            _size = size;
            CardSize = size;
        }
        public SpeciesType SpeciesType { get { return _speciesType; } }
        public ImageObject ImageCard { get { return _imageObject; } }
        public int CardPrice { get; set; }
        public Size CardSize { get; set; }
        public int StartX { get; set; }
        public int StartY { get; set; }
        public float ZoomX { get; set; }
        public float ZoomY { get; set; }
        public int Index {  get; set; }
        public event DrawCardDelegate DrawCardCallback;
        public void UpdateSize(float zoomX, float zoomY)
        {
            ZoomX = zoomX;
            ZoomY = zoomY;
            CardSize = new Size((int)(_size.Width * zoomX), (int)(_size.Height * zoomY));
        }
        protected bool _isMouseDown = false;
        public virtual void MouseDown(object sender, MouseEventArgs e)
        {
            var rect = new Rectangle(StartX, StartY, CardSize.Width, CardSize.Height);
            if (rect.Contains(e.Location))
                _isMouseDown = true;
        }
        protected Point _curCard = Point.Empty;
        public virtual void MouseMove(object sender, MouseEventArgs e)
        {
            _curCard = e.Location;
        }
        public virtual void MouseUp(object sender, MouseEventArgs e)
        {
            if (_isMouseDown && DrawCardCallback != null)
                DrawCardCallback(this, new CardEventArgs(_curCard.X - (CardSize.Width >> 1), _curCard.Y - (CardSize.Height >> 1)));

            _isMouseDown = false;
        }
        protected int _cardX;
        protected int _cardY;
        public virtual void DrawCard(Graphics g, Font font, Brush brush)
        {
            IntPtr hdc = g.GetHdc();
            DrawCard(hdc);
            DrawMoveImage(hdc);
            g.ReleaseHdc(hdc);
            DrawString(g, font, brush);
        }
        protected virtual void DrawMoveImage(IntPtr hdc)
        {
            if (_isMouseDown)
            {
                int x = _curCard.X - (CardSize.Width >> 1);
                int y = _curCard.Y - (CardSize.Height >> 1);
                GDI32.DrawImageTransparenc(hdc, _imageObject.BitmapPtr, x, y, _imageObject.Width, _imageObject.Height, CardSize.Width, CardSize.Height);
            }
        }
        protected virtual void DrawCard(IntPtr hdc)
        {
            GDI32.DrawImageTransparenc(hdc, _imageObject.BitmapPtr, StartX, StartY, _imageObject.Width, _imageObject.Height, CardSize.Width, CardSize.Height);
        }
        protected virtual void DrawString(Graphics g, Font font, Brush brush)
        {
            string drawString = this.CardPrice.ToString();
            SizeF sf = drawString.MeasureString(font);
            var qqqq = StartX + (CardSize.Width - sf.Width) / 2;
            g.DrawString(drawString, font, brush, StartX + (CardSize.Width - sf.Width) / 2, StartY + CardSize.Height + 2 * ZoomY);
        }
    }
    public class CardEventArgs : EventArgs
    {
        public CardEventArgs(int x, int y)
        {
            this.X = x;
            this.Y = y;
        }
        public int X { get; set; }
        public int Y { get; set; }
    }
}
